#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define BLOCK_SIZE 64


typedef struct PT
{
    int pag;
    int effective;
    unsigned short block;
    int modify;
    unsigned short address;
    struct PT *next;
}PT;
typedef struct OL
{
    char operation[3];
    int pag;
    unsigned short move;
    struct OL *next;
}OL;

void sc(struct PT *L)
{   
    PT *r;OL *l;
    r = (PT*)malloc(sizeof(PT));
    r = L->next;
 
    printf("pag  effective  block  modify  address\n");
    printf("--------------------------------------\n");
    while(r!= NULL){
         printf("%d    %d      	%d	%d	%d\n",r->pag,r->effective,r->block,r->modify,r->address);
         r = r->next;
    }
    
}
int main(int argc,char *argv[])
{
    // 文件
    char *f1,*f2;
    f1 = argv[1];
    FILE *fp1 = fopen(f1, "r");
    f2 = argv[2];
    FILE *fp2 = fopen(f2, "r");
    if(!freopen(f1,"r",stdin)){
        fprintf(stderr,"could not redirect stdin from file %s\n",f1);
        exit(2);
    }
    if(!freopen(f2,"r",stdin)){
        fprintf(stderr,"could not redirect stdin from file %s\n",f2);
        exit(3);
    }
    if(argc!=3){
        fprintf(stderr,"参数数量错误，");
        exit(1);
    }
    
    
    PT *pt,*p,*p1;
    pt = (PT*)malloc(sizeof(PT));
    p = (PT*)malloc(sizeof(PT));
    pt->next = NULL;
    p = pt;
    
    OL *ol,*o,*o1;
    ol = (OL*)malloc(sizeof(OL));
    ol->next = NULL;
    o = (OL*)malloc(sizeof(OL));
    o = ol;
    
    
  
    while(!feof(fp1))
    {
        p1 = (PT*)malloc(sizeof(PT));
        fscanf(fp1,"%d %d %hd %d %hd ",&p1->pag,&p1->effective,&p1->block,&p1->modify,&p1->address);
        
        p->next = p1;
        p = p1;
    }
    p->next = NULL;
    
    while(!feof(fp2))
    {
        o1 = (OL*)malloc(sizeof(OL));
        fscanf(fp2,"%s %d %hd ",&o1->operation[0],&o1->pag,&o1->move);
        o->next = o1;
        o = o1; 
    }
    o->next = NULL;
    
    
    
    OL *r;
    PT *l;
    l = (PT*)malloc(sizeof(PT));
    r = (OL*)malloc(sizeof(OL));
    r = ol->next;
    while(r!=NULL)
    {
        printf("操作：%s，页号：%d:",r->operation,r->pag);
        
        l = pt->next;
        while(l->next!=NULL)
        {
            if(r->pag==l->pag)
            break;
            l = l->next;
        }
        if(l->effective == 1){
           
            unsigned int absAddr = 0;
            absAddr = l->block*BLOCK_SIZE + r->move;
            printf("绝对地址:0x%04X\n",absAddr);
            
            
        }else{
            printf("该页号不在主存，产生缺页中断！\n");
            
        }
        if(strcmp(r->operation,"存")==0 && l->effective==1)
            l->modify = 1;
        r = r->next; 
  }
    
}